package com.msmall.community.wiki.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.keyword.model.Keyword;

public class WikiBabyMotherPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(WikiBabyMotherPreparer.class);
	
	@SuppressWarnings("unchecked")
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Short keywordType = 0;
			Long segid = null;
			Long segType = null;
			if(context.getAttribute("keywordType") != null){
				keywordType = (Short) context.getAttribute("keywordType").getValue();
			}
			if(context.getAttribute("segid") != null){
				segid = (Long) context.getAttribute("segid").getValue();
			}
			if(context.getAttribute("segType") != null){
				segType = (Long) context.getAttribute("segType").getValue();
			}
			Map<Short, List<Keyword>> maps = (Map<Short, List<Keyword>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KEYWORD);
			try{
				if(keywordType == null || keywordType == 0){
					CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
					CascadeCollection<Segment> topParent = cs.getAllDatas().get(segType);
					CascadeCollection<Segment> current = cs.getAllDatas().get(segid);
					Long babyOrMother = null;
					if(ConstantDB.SEGMENT_ID_NAV.equals(segType)){
						if(current.getFirstParent() != null && current.getFirstParent().getCurrent().getSegmentid().compareTo(segType) == 0){
							babyOrMother = current.getCurrent().getSegmentid();
						}else if(current.getFirstParent() !=null && current.getFirstParent().getFirstParent() != null
								&& current.getFirstParent().getFirstParent().getCurrent().getSegmentid().compareTo(segType) == 0){
							babyOrMother = current.getFirstParent().getCurrent().getSegmentid();
						}else if(current.getFirstParent() !=null && current.getFirstParent().getFirstParent() != null && current.getFirstParent().getFirstParent().getFirstParent() != null
								&& current.getFirstParent().getFirstParent().getFirstParent().getCurrent().getSegmentid().compareTo(segType) == 0){
							babyOrMother = current.getFirstParent().getFirstParent().getFirstParent().getCurrent().getSegmentid();
						}
						if(babyOrMother != null){
							if(babyOrMother > ConstantDB.SEGMENT_ID_KLG_MOTHER_MAX){
								keywordType = ConstantDB.KEYWORD_TYPE_SITE_INDEX_WIKI_BABY;
							}else{
								keywordType = ConstantDB.KEYWORD_TYPE_SITE_INDEX_WIKI_MOTHER;
							}
						}
					}else if(ConstantDB.SEGMENT_ID_WIKI_NAV.equals(segType)){
						
					}
				}
			}catch(Exception e){
				log.debug(e);
			}
			if(keywordType == null || keywordType == 0){
				keywordType = ConstantDB.KEYWORD_TYPE_SITE_WIKI_MOTHER_BABY;
			}
			request.setAttribute("babyMotherKeywords", maps.get(keywordType));
		}catch(Exception e){
			log.debug(e);
		}
	}
}
